package com.sumer.brain.common.security.handler;

import com.alibaba.fastjson2.JSON;
import com.sumer.brain.common.constant.SystemConstant;
import com.sumer.brain.common.enums.HttpStatus;
import com.sumer.brain.common.model.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CustomerAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        R<Object> result = R.build(HttpStatus.UNAUTHORIZED.getCode(), HttpStatus.UNAUTHORIZED.getMsg(), authException.getMessage());
        response.setContentType(SystemConstant.CONTENT_TYPE);
        response.getWriter().write(JSON.toJSONString(result));
    }
}
